#include <cstdio>

long long fac[ 100000 ];
int n, m, p;

void init( )
{
    int i;
    scanf("%d%d%d", &n, &m, &p);
    fac[ 0 ] = 1;
    for ( i = 1; i < p; i++ )
        fac[ i ] = fac[ i - 1 ] * i % p;
}

long long mypow( long long x, int n, int p )
{
    int pos = 1;
    long long ret = 1;
    while ( pos <= n ) pos <<= 1;
    pos >>= 1;
    while ( pos )
    {
        ret = ret * ret % p;
        if ( pos & n ) ret = ret * x % p;
        pos >>= 1;
    }
    return ret;
}

long long C( int n, int m, int p )
{
    if ( m > n ) return 0;
    return fac[ n ] * mypow( fac[ m ] * fac[ n - m ] % p, p - 2, p ) % p;
}

long long lucas( int n, int m, int p )
{
    if ( m == 0 ) return 1;
    return lucas( n / p, m / p, p ) * C( n % p, m % p, p ) % p;
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        printf("%I64d\n", lucas( n + m, m, p ));
    }
    return 0;
}
